package net.sourceforge.krama.tag;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import net.sourceforge.krama.css.GenerateCSS;
import net.sourceforge.krama.exception.InvalidOnClickCall;
import net.sourceforge.krama.introspection.TIntrospection;
import net.sourceforge.krama.js.GenerateJS;
import net.sourceforge.krama.servlet.TAjaxServlet;
import net.sourceforge.krama.util.TUtil;
import net.sourceforge.krama.xml.TXMLLoader;

import org.jdom.Document;

import sal.demo.View;

/**
 * 
 * @author chabane
 *
 */
public class TLib extends BodyTagSupport implements ILib {

	private static final long serialVersionUID = -5470383217236484806L;
	private String jsPage;
	private String cssPage;
	private static String configXML;
	private ServletConfig config = null;
	private String view;
	private String viewbean;
	private GenerateJS generateCode = new GenerateJS();
	private GenerateCSS generateCSS = new GenerateCSS();
	private String jsCode;
	public static String css = "";
	private String type;
	
	public void setType(String type) {
		this.type = type;
	}

	public String getViewbean() {
		return viewbean;
	}

	public void setViewbean(String viewbean) {
		this.viewbean = viewbean;
	}

	private Object object;

	public String getJsPage() {
		return jsPage;
	}

	public void setJsPage(String jsPage) {
		this.jsPage = jsPage;
	}

	public String getCssPage() {
		return cssPage;
	}

	public void setCssPage(String cssPage) {
		this.cssPage = cssPage;
	}

	private void config() throws IOException {

		configXML = getConfig().getServletContext().getInitParameter(
				"configKrama");
		configXML = getConfig().getServletContext().getRealPath(configXML);

		TXMLLoader kramaInstance = TXMLLoader.getInstance();
		Document document = kramaInstance.openkramaConfig(configXML);
		setCssPage(kramaInstance.parsingkramaConfigCSSPageName(document));
		setJsPage(kramaInstance.parsingkramaConfigJSPageName(document));
		setView(kramaInstance.parsingkramaConfigViewName(document, viewbean));
	
		setJsPage(getConfig().getServletContext().getRealPath(getJsPage()));
		setCssPage(getConfig().getServletContext().getRealPath(getCssPage()));
	}

	public int doStartTag() throws JspException {

		try {
			config();
			ILib lib = null;
			
			if(viewbean.endsWith("Ajax"))
				lib = (TAjaxView) TIntrospection.getView(getView());
			else
				lib = (TView) TIntrospection.getView(getView());
			
			css = lib.start(object, pageContext);
			
			/*
			 * Get all javascript code necessary to execute a current view
			 */
			setJsCode(generateCode.getCode());
			
			/*
			 * Write a javascript code on krama.js
			 */
			TUtil.writeInFile(getJsPage(), jsCode);
			
			/*
			 * If kind of view is not AjaxView
			 */
			if(type == null){
				TUtil.writeInFile(getCssPage(), css);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Tag.EVAL_BODY_INCLUDE;
	}

	public int doAfterBody() throws JspException {

		return Tag.SKIP_BODY;
	}

	public Object getObject() {
		return this.object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public ServletConfig getConfig() {
		return config;
	}

	public void setConfig(ServletConfig config) {
		this.config = config;
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	public String start(Object obj, PageContext pageContext) throws IOException {
		return "";
	}

	public String getJsCode() {
		return jsCode;
	}

	public void setJsCode(String jsCode) {
		this.jsCode = jsCode;
	}

	public static String getConfigXML() {
		return configXML;
	}
}
